package com.gscitysfy.cus.drain.modules.reserveplan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gscitysfy.cus.drain.modules.reserveplan.dto.DrainReservePlanMnTeamDto;
import com.gscitysfy.cus.drain.modules.reserveplan.entity.DrainReservePlanMnMaterial;
import com.gscitysfy.cus.drain.modules.reserveplan.entity.DrainReservePlanMnTeam;
import com.gscitysfy.cus.drain.modules.reserveplan.mapper.DrainReservePlanMnMaterialMapper;
import com.gscitysfy.cus.drain.modules.reserveplan.mapper.DrainReservePlanMnTeamMapper;
import com.gscitysfy.cus.drain.modules.reserveplan.service.IDrainReservePlanMnTeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cus.drain.modules.resource.dto.ResourceTeamDto;
import com.gscitysfy.cus.drain.modules.resource.entity.ResourceTeam;
import com.gscitysfy.cus.drain.modules.resource.service.IResourceTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 预警对应四个等级的应急队伍 服务实现类
 * </p>
 *
 * @author shenglin
 * @since 2022-11-15 14:28:05
 */
@Service
public class DrainReservePlanMnTeamServiceImpl extends ServiceImpl<DrainReservePlanMnTeamMapper, DrainReservePlanMnTeam> implements IDrainReservePlanMnTeamService {

    @Autowired
    private DrainReservePlanMnTeamMapper drainReservePlanMnTeamMapper;

    @Autowired
    private  IResourceTeamService iResourceTeamService;

    @Override
    public void deletByInfoId(List<String> infoIds) {
        LambdaQueryWrapper<DrainReservePlanMnTeam> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DrainReservePlanMnTeam::getInfoId,infoIds);
        List<DrainReservePlanMnTeam> drainReservePlanMnTeams = drainReservePlanMnTeamMapper.selectList(queryWrapper);
        for (DrainReservePlanMnTeam drainReservePlanMnTeam : drainReservePlanMnTeams) {
            drainReservePlanMnTeam.setCmUsable(true);
        }
        this.updateBatchById(drainReservePlanMnTeams);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int saveBathReservePlanMnTeamInfo(List<DrainReservePlanMnTeam> drainReservePlanMnTeams) {
        if(!drainReservePlanMnTeams.isEmpty()){
            //删除原有的应急队伍信息
            LambdaQueryWrapper<DrainReservePlanMnTeam> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DrainReservePlanMnTeam::getInfoId,drainReservePlanMnTeams.get(0).getInfoId());
            List<DrainReservePlanMnTeam> old = drainReservePlanMnTeamMapper.selectList(queryWrapper);
            for (DrainReservePlanMnTeam drainReservePlanMnTeam : old) {
                drainReservePlanMnTeam.setCmUsable(true);
            }
            this.updateBatchById(old);
            int i=0;
            for (DrainReservePlanMnTeam reservePlanMnTeam : drainReservePlanMnTeams) {
                //预案应急队伍的预案id不能为空
                if(reservePlanMnTeam!=null&& reservePlanMnTeam.getInfoId()!=null){
                    i+=drainReservePlanMnTeamMapper.insert(reservePlanMnTeam);
                }
            }
            return i;
        }
        return 0;
    }

    @Override
    public List<DrainReservePlanMnTeamDto> getReservePlanMnTeamInfoList(String infoId,String level) {
        LambdaQueryWrapper<DrainReservePlanMnTeam> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainReservePlanMnTeam::getInfoId,infoId);
        queryWrapper.eq(DrainReservePlanMnTeam::getCmUsable,false);
        if(StringUtils.isNotBlank(level)){
            queryWrapper.eq(DrainReservePlanMnTeam::getLevel,level);
        }
        List<DrainReservePlanMnTeam> drainReservePlanMnTeams = drainReservePlanMnTeamMapper.selectList(queryWrapper);
        List<DrainReservePlanMnTeamDto> result = new ArrayList<>();
        //根据应急队伍的id查询应急队伍的信息
        for (DrainReservePlanMnTeam drainReservePlanMnTeam : drainReservePlanMnTeams) {
            ResourceTeamDto byId = iResourceTeamService.getById(drainReservePlanMnTeam.getMnTeamId());
            if(byId!=null){
                DrainReservePlanMnTeamDto dto=new DrainReservePlanMnTeamDto();
                //获取应急队伍地址
                dto.setAddress(byId.getAddress());
                //获取应急队伍名称
                dto.setTeamName(byId.getTeamName());
                //获取应急队伍联系电话
                dto.setPhone(byId.getPhone());
                //获取应急队伍队长名称
                dto.setManagePerson(byId.getManagePerson());
                //获取应急队伍的id
                dto.setMnTeamId(drainReservePlanMnTeam.getMnTeamId());
                //获取当前预案id
                dto.setInfoId(drainReservePlanMnTeam.getInfoId());
                //获取当前应急等级
                dto.setLevel(drainReservePlanMnTeam.getLevel());
                dto.setCmKid(drainReservePlanMnTeam.getCmKid());
                result.add(dto);
            }
        }
        return result;
    }

}
